package com.lipl.bo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lipl.entity.User;
import com.lipl.mapper.UserDAO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class UserInfoImpl extends ServiceImpl<UserDAO, User> implements UserInfoService{
    @Resource
    UserDAO userDAO;
    static ExecutorService executor = Executors.newFixedThreadPool(4);
    @Override
    public User queryListAll() {

        long sum = userDAO.selectCount(null);
        long start = System.currentTimeMillis();
        for (int i =0;i<=sum/20000;i++){
            int finalI = i;
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    long start11 = System.currentTimeMillis();
                    IPage<User> userIPage = userDAO.selectPage(new Page<>(finalI +1,20000, sum),new QueryWrapper());
                    System.out.println("查询第"+(finalI +1)+"批数据的时间为===========" +(System.currentTimeMillis()-start11)/1000+"s");
                    if(userIPage.getRecords()!=null && userIPage.getRecords().size()>0){
                        userDAO.insertBatch(userIPage.getRecords());
                    }
                    System.out.println("===========第"+(finalI +1)+"批数据查询插入时间为：" +(System.currentTimeMillis()-start11)/1000+"s ===========");
                }
            });
        }
        executor.shutdown();
        while(!executor.isTerminated()){
            //System.out.print("-");
        };
        System.out.println(sum+"条数据查询插入的总时间为===========" +(System.currentTimeMillis()-start)/1000+"s");

        return null;
    }

    @Override
    public User queryList() {

        long sum = userDAO.selectCount(null);
        long start = System.currentTimeMillis();
        for (int i =0;i<=sum/20000;i++){
            long start11 = System.currentTimeMillis();
            IPage<User> userIPage = userDAO.selectPage(new Page<>( i+1,20000, sum),new QueryWrapper());
            System.out.println("查询时间===========" +(System.currentTimeMillis()-start11)+"ms");
            if(userIPage.getRecords()!=null && userIPage.getRecords().size()>0){
                userDAO.insertBatch(userIPage.getRecords());
            }
            System.out.println("===========第"+(i+1)+"批数据查询插入时间为：" +(System.currentTimeMillis()-start11)+"ms ===========");
        }
        System.out.println(new Date()+":"+sum+"条数据查询插入的总时间为===========" +(System.currentTimeMillis()-start)/1000/60+"min");

        return null;
    }
}
